<?php

namespace common\models;

use common\helpers\StringUtil;
use Yii;

/**
 * This is the model class for table "department".
 *
 * @property integer $id
 * @property string $name
 * @property string $number
 * @property string $display_name
 * @property string $layer
 * @property integer $is_company
 * @property integer $parent_id
 * @property string $parent_number
 * @property integer $qywx_id
 * @property integer $parent_qywx_id
 * @property integer $order
 * @property string $memo
 * @property string $hr_id
 * @property integer $hr_status
 * @property string $mdm_id
 * @property string $parent_mdm_id
 * @property integer $mdm_status
 * @property string $kd_id
 * @property string $parent_kd_id
 * @property string $created_by
 * @property string $created_at
 * @property string $updated_by
 * @property string $updated_at
 * @property integer $root
 * @property integer $lft
 * @property integer $rgt
 * @property integer $lvl
 * @property string $icon
 * @property integer $icon_type
 * @property integer $active
 * @property integer $selected
 * @property integer $disabled
 * @property integer $readonly
 * @property integer $visible
 * @property integer $collapsed
 * @property integer $movable_u
 * @property integer $movable_d
 * @property integer $movable_l
 * @property integer $movable_r
 * @property integer $removable
 * @property integer $removable_all
 */
class Department extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'department';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['is_company', 'parent_id', 'qywx_id', 'parent_qywx_id', 'order', 'hr_status', 'mdm_status', 'root', 'lft', 'rgt', 'lvl', 'icon_type', 'active', 'selected', 'disabled', 'readonly', 'visible', 'collapsed', 'movable_u', 'movable_d', 'movable_l', 'movable_r', 'removable', 'removable_all'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['name', 'number', 'memo', 'icon'], 'string', 'max' => 255],
            [['display_name'], 'string', 'max' => 1024],
            [['layer', 'parent_number', 'hr_id', 'mdm_id', 'parent_mdm_id', 'kd_id', 'parent_kd_id', 'created_by', 'updated_by'], 'string', 'max' => 45],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'name' => Yii::t('app', 'Name'),
            'number' => Yii::t('app', 'Number'),
            'display_name' => Yii::t('app', 'Display Name'),
            'layer' => Yii::t('app', 'Layer'),
            'is_company' => Yii::t('app', 'Is Company'),
            'parent_id' => Yii::t('app', 'Parent ID'),
            'parent_number' => Yii::t('app', 'Parent Number'),
            'qywx_id' => Yii::t('app', 'Qywx ID'),
            'parent_qywx_id' => Yii::t('app', 'Parent Qywx ID'),
            'order' => Yii::t('app', 'Order'),
            'memo' => Yii::t('app', 'Memo'),
            'hr_id' => Yii::t('app', 'Hr ID'),
            'hr_status' => Yii::t('app', 'Hr Status'),
            'mdm_id' => Yii::t('app', 'Mdm ID'),
            'parent_mdm_id' => Yii::t('app', 'Parent Mdm ID'),
            'mdm_status' => Yii::t('app', 'Mdm Status'),
            'kd_id' => Yii::t('app', 'Kd ID'),
            'parent_kd_id' => Yii::t('app', 'Parent Kd ID'),
            'created_by' => Yii::t('app', 'Created By'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_by' => Yii::t('app', 'Updated By'),
            'updated_at' => Yii::t('app', 'Updated At'),
            'root' => Yii::t('app', 'Root'),
            'lft' => Yii::t('app', 'Lft'),
            'rgt' => Yii::t('app', 'Rgt'),
            'lvl' => Yii::t('app', 'Lvl'),
            'icon' => Yii::t('app', 'Icon'),
            'icon_type' => Yii::t('app', 'Icon Type'),
            'active' => Yii::t('app', 'Active'),
            'selected' => Yii::t('app', 'Selected'),
            'disabled' => Yii::t('app', 'Disabled'),
            'readonly' => Yii::t('app', 'Readonly'),
            'visible' => Yii::t('app', 'Visible'),
            'collapsed' => Yii::t('app', 'Collapsed'),
            'movable_u' => Yii::t('app', 'Movable U'),
            'movable_d' => Yii::t('app', 'Movable D'),
            'movable_l' => Yii::t('app', 'Movable L'),
            'movable_r' => Yii::t('app', 'Movable R'),
            'removable' => Yii::t('app', 'Removable'),
            'removable_all' => Yii::t('app', 'Removable All'),
        ];
    }

    public function getPrintName(){
        return sprintf('部门: %s 编号: %s', $this->name, $this->number);
    }

    public function setLayer($layer){
        $this->layer = $layer;
        if (strcmp($this->layer, '集团') == 0){
            $this->is_company = 1;
            return;
        }
        if (strcmp($this->layer, '公司') == 0){
            $this->is_company = 1;
            return;
        }
        $this->is_company = 0;
    }
}
